/**
 *  Copyright (C) 2010  Thomas Volk
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.bookmarks.core.service;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.bookmarks.core.beans.ContentType;
import com.googlecode.bookmarks.core.beans.MimeType;
import com.googlecode.bookmarks.core.exceptions.EntityNotFoundException;

@Transactional
@Service
public class ContentTypeServiceImpl extends AbstractPersistenceService
    implements ContentTypeService {
    
    public ContentType findContentTypeByName(String name) throws EntityNotFoundException{
    	Query query = getEntityManager().createNamedQuery("findContentTypeByName");
    	query.setParameter("name", name);
    	try {
    		return (ContentType) query.getSingleResult();
    	}
    	catch (NoResultException e) {
			throw new EntityNotFoundException(e);
		}
    }
    
    public MimeType findMimeTypeTypeByTypes(String mimeTypes) throws EntityNotFoundException{
    	MimeType mimeType = new MimeType(mimeTypes);
    	return findMimeTypeTypeByTypes(mimeType.getType(), mimeType.getSubType());
    }

	public MimeType findMimeTypeTypeByTypes(String type, String subType) throws EntityNotFoundException{
    	Query query = getEntityManager().createNamedQuery("findMimeTypeByTypes");
    	query.setParameter("type", type);
    	query.setParameter("subType", subType);
    	try {
    		return (MimeType) query.getSingleResult();
    	}
    	catch (NoResultException e) {
			throw new EntityNotFoundException(e);
		}
	}
}
